Ext.require(['Ext.data.*', 'Ext.grid.*']);

Ext.define('Person', {
    extend: 'Ext.data.Model',
    fields: [{
        name: 'id',
        useNull: true
    }, 'type', 'text', 'Remark', 'ParentID', 'IsGroup', 'orderID'],
    validations: [{
        type: 'length',
        field: 'text',
        min: 1
    }, {
        type: 'length',
        field: 'type',
        min: 1
    }, {
        type: 'length',
        field: 'orderID',
        min: 1
    }]
});
//所属部门
Ext.define('bmlist', {
    extend: 'Ext.data.Model',
    fields: ['id', 'dwCode', 'dwName']
});
Ext.onReady(function(){
var dwtype;
var treestore = Ext.create('Ext.data.TreeStore', {
    root: {
        expanded: true
    },
    proxy: {
        type: 'ajax',
        url: 'tree-data-zdfl.xml'
        
    }
    
});
var treePanel = Ext.create('Ext.tree.Panel', {
    id: 'tree-panel',
    title: '字典分类',
    region: 'west',
    split: true,
    width: 250,
    minSize: 150,
    rootVisible: false,
    collapsible: true,
    floatable: true,
    autoScroll: true,
    store: treestore
});

treePanel.getSelectionModel().on('select', function(selModel, record) {
    if (record.get('leaf')) {
        //             Ext.getCmp('content-panel').layout.setActiveItem(record.getId() + '-panel');
        var col=Ext.getCmp('colgsbm');
        if(record.getId()=='专业分类')
            col.show();
        else
            col.hide();
        store.clearFilter(false);
        dwtype = record.getId()
        store.filter("type",dwtype);
        store.load();
    }
});
var bmStore = Ext.create('Ext.data.Store', {
    model: 'bmlist',
    autoLoad: true,
    //autoSync: true,
    queryMode: 'local',
    proxy: {
        type: 'ajax',
        url: 'yhpcOrgAjax.aspx?action=read&type=公司部门',
        reader: {
            type: 'json',
            root: 'data'
        }
    }

});  
    var store = Ext.create('Ext.data.Store', {
        //autoLoad: true,
        autoSync: true,
        model: 'Person',
        proxy: {
            type: 'ajax',
            api: {
            read: 'yhpcDictionaryAjax.aspx?action=read',
            create: 'yhpcDictionaryAjax.aspx?action=create',
            update: 'yhpcDictionaryAjax.aspx?action=update',
            destroy: 'yhpcDictionaryAjax.aspx?action=delete'
            },
            reader: {
                type: 'json' ,
                root: 'data'
            },
            writer: {
                type: 'json' ,
                root: 'data'      
            }},
        listeners: {
            write: function(store, operation){
//                var record = operation.getRecords()[0],
//                    name = Ext.String.capitalize(operation.action),
//                    verb;
//                    
//                    
//                if (name == 'Destroy') {
//                    record = operation.records[0];
//                    verb = 'Destroyed';
//                } else {
//                    verb = name + 'd';
//                }
                //Ext.example.msg(name, Ext.String.format("{0} user: {1}", verb, record.getId()));
                
            }
        }
    });

    var rowEditing = Ext.create('Ext.ux.grid.plugin.RowEditing');
    
    var grid = Ext.create('Ext.grid.Panel', {
        //renderTo: document.body,
        plugins: [rowEditing],
        //width: 400,
        //height: 300,
        region: 'center',
        frame: true,
        title: '字典维护',
        store: store,
        //iconCls: 'icon-user',
        defaults: { xtype: 'textfield' }, 
        columns: [{
            text: '序号',

            sortable: true,
            width:60,
            dataIndex: 'orderID',
            emptyText: '填写序号',
            
            field: {
            xtype: 'numberfield'
            }
        }, {
            header: '分类',
            emptyText: '选择分类',
            width: 80,
            sortable: true,
            dataIndex: 'type',
            editor: {
                xtype: 'combobox',
                //typeAhead: true,
                //triggerAction: 'all',
                //selectOnTab: true,
                store: [
                    ['隐患来源', '隐患来源'],
                    ['专业分类', '专业分类'],
                    ['可能导致后果', '可能导致后果'],
                    ['奖励金额', '奖励金额'],
                    ['考核金额', '考核金额']
                ]
                //,
                //lazyRender: true,
                //listClass: 'x-combo-list-small'
            }
        }, {
            text: '名称',
            sortable: true,
            flex: 1,
            width:100,
            dataIndex: 'text',
            field: {
                xtype: 'textfield'
            }
        }, {
            text: '规属部门',
            sortable: true,
            flex: 1,
            id:'colgsbm',
            width: 100,
            dataIndex: 'Remark',
            editor: {
                xtype: 'combobox',
                typeAhead: true,
                selectOnTab: true,
                displayField: 'dwName',
                valueField: 'dwName',
                store: bmStore,
                lazyRender: true,
                listClass: 'x-combo-list-small'
            }
        }],
        dockedItems: [{
            xtype: 'toolbar',
            items: [{
                text: '增加',
                iconCls: 'icon-add',
                handler: function(){
                    // empty record
                    store.insert(0, new Person({type:dwtype,orderID:0}));
                    rowEditing.startEdit(0, 2);
                }
            }, '-', {
                itemId: 'delete',
                text: '删除',
                iconCls: 'icon-delete',
                disabled: true,
                handler: function(){
                    var selection = grid.getView().getSelectionModel().getSelection()[0];
                    if (selection) {
                        store.remove(selection);
                    }
                }
            }]
        }]
    });
    grid.getSelectionModel().on('selectionchange', function(selModel, selections){
        grid.down('#delete').setDisabled(selections.length === 0);
    });
    

    Ext.create('Ext.Viewport', {
        layout: {
            type: 'border',
            padding: 5
        },
        defaults: {
            split: true
        },
        items: [treePanel, grid]
    });
});
